<?php

include("mysql_conn.php");
include("settings.php");

$username=$_GET["username"];
$stylestring=file_get_contents("style-report.css");

$now=time();

function drawrow($row)
{
 echo $row['updateinfo'] . "\n";
}

// get the updatedate of the oldest update for all current projects 
// (the ones being reported on)
$result=mysql_query("select date_format(min(updatedate), '%m/%d/%Y') as startdate from updates u, projects p where u.projectid=p.projectid and p.username='$username' and p.state='1' and u.isold='0' and u.updatetype='T'");

while($row=mysql_fetch_array($result))
{
 $startdate=$row['startdate'];
}

// get the user's full name
$result=mysql_query("select fullname from users where username='$username'");

while($row=mysql_fetch_array($result))
{
 $fullname=$row['fullname'];
}

// draw a header
// include style info so there is no dependent CSS file
echo "<html><head><title>$site_title: Report</title><style type='text/css'>$stylestring</style></head>
<h3>$site_title Report</h3><br>";

 // header for each user
 echo "<p><hr><p><b>Status Report For: </b> $fullname<br>";
 echo "<b>Status Period:</b> $startdate to " . date("m/d/Y", $now);
 // non-projects and updates
 echo "<h3>Non-Project Related Activities</h3>";

 // this really ought to return one projectid
 $nonproj=mysql_query("select projectid from projects where state='4' and username='$username'");
 while($row=mysql_fetch_array($nonproj)) 
 {
  $nonprojid=$row['projectid'];
  
  echo "<table>";
  echo "<tr><th align=left>Tasks Completed this week:</th></tr>";
 
  # $result2=mysql_query("select updatetext from updates where projectid='$nonprojid' and updatetype='T' and date(updatetime) >= current_date - interval 7 day or projectid='$nonprojid' and updatetype='T' and isold='0'");
  $result2=mysql_query("select concat(updatetext,' (',date_format(updatedate,'%m/%d/%Y'),')') as updateinfo from updates where projectid='$nonprojid' and updatetype='T' and isold='0' order by updatedate");
  echo "<tr><td><pre>";
  
  while($row=mysql_fetch_array($result2))
  {
   drawrow($row);
  }
  echo "</pre></td></tr>";
  echo "<tr><th align=left>Obstacles:</th></tr>";

  # $result3=mysql_query("select updatetext from updates where projectid='$nonprojid' and updatetype='O' and date(updatetime) >= current_date - interval 7 day or projectid='$nonprojid' and updatetype='O' and isold='0'");
  $result3=mysql_query("select concat(updatetext,' (',date_format(updatedate,'%m/%d/%Y'),')') as updateinfo from updates where projectid='$nonprojid' and updatetype='O' and isold='0' order by updatedate");
  echo "<tr><td><pre>";

  while($row=mysql_fetch_array($result3))
  {
   drawrow($row);
  }
  echo "</pre></td></tr>";

  echo "<tr><th align=left>Next Steps:</th></tr>";

  # $result4=mysql_query("select updatetext from updates where projectid='$nonprojid' and updatetype='N' and date(updatetime) >= current_date - interval 7 day or projectid='$nonprojid' and updatetype='N' and isold='0'");
  $result4=mysql_query("select concat(updatetext,' (',date_format(updatedate,'%m/%d/%Y'),')') as updateinfo from updates where projectid='$nonprojid' and updatetype='N' and isold='0' order by updatedate");
  echo "<tr><td><pre>";

  while($row=mysql_fetch_array($result4))
  {
   drawrow($row);
  }
  echo "</pre></td></tr>";

  echo "</table><p>";
 } 

 // projects and updates
 // active projects only, please
 $proj=mysql_query("select projectid from projects where state='1' and username='$username'");

 while($row=mysql_fetch_array($proj))  
 {
  $projectid=$row['projectid'];

  $result5=mysql_query("select title,date_format(origduedate, '%m/%d/%Y') as origduedate,date_format(currduedate, '%m/%d/%Y') as currduedate from projects where projectid='$projectid'");
  
  echo "<table><tr><td>";
  while($row=mysql_fetch_array($result5))
  {
   echo "<b>Project Name:</b>" . $row['title'] . "<br>";
   echo "<b>Original Due Date:</b>" . $row['origduedate'] . "<br>";
   echo "<b>Current Due Date:</b>" . $row['currduedate'] . "<br>";
  }
  echo "</td></tr>";

  echo "<tr><th align=left>Tasks Completed this week:</th></tr>";
 
  # $result6=mysql_query("select updatetext from updates where projectid='$projectid' and updatetype='T' and date(updatetime) >= current_date - interval 7 day or projectid='$projectid' and updatetype='T' and isold='0'");
  $result6=mysql_query("select concat(updatetext,' (',date_format(updatedate,'%m/%d/%Y'),')') as updateinfo from updates where projectid='$projectid' and updatetype='T' and isold='0' order by updatedate");
  echo "<tr><td><pre>";

  while($row=mysql_fetch_array($result6))
  {
   drawrow($row);
  }
  echo "</pre></td></tr>";

  echo "<tr><th align=left>Obstacles:</th></tr>";

  # $result7=mysql_query("select updatetext from updates where projectid='$projectid' and updatetype='O' and date(updatetime) >= current_date - interval 7 day or projectid='$projectid' and updatetype='O' and isold='0'");
  $result7=mysql_query("select concat(updatetext,' (',date_format(updatedate,'%m/%d/%Y'),')') as updateinfo from updates where projectid='$projectid' and updatetype='O' and isold='0' order by updatedate");
  echo "<tr><td><pre>";

  while($row=mysql_fetch_array($result7))
  {
   drawrow($row);
  }
  echo "</pre></td></tr>";

  echo "<tr><th align=left>Next Steps:</th></tr>";

  # $result8=mysql_query("select updatetext from updates where projectid='$projectid' and updatetype='N' and date(updatetime) >= current_date - interval 7 day or projectid='$projectid' and updatetype='N' and isold='0'");
  $result8=mysql_query("select concat(updatetext,' (',date_format(updatedate,'%m/%d/%Y'),')') as updateinfo from updates where projectid='$projectid' and updatetype='N' and isold='0' order by updatedate");
  echo "<tr><td><pre>";

  while($row=mysql_fetch_array($result8))
  {
   drawrow($row);
  }
  echo "</pre></td></tr>";
  echo "</table><p>";
 }
